home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
gnu
/
gawk
/
cawf2st.zoo
/
cawf.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-04-12
|
14KB
|
408 lines
/*
* cawf - a C version of Henry Spencer's awf(1), the Amazingly
* Workable (text) Formatter
*
* V. Abell, Purdue University Computing Center
* Version 1.0, February, 1991
*/
/*
* Copyright (c) 1991 Purdue University Research Foundation,
* West Lafayette, Indiana 47907. All rights reserved.
*
* Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
* University Computing Center. Not derived from licensed software;
* derived from awf(1) by Henry Spencer of the University of Toronto.
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to alter it and redistribute
* it freely, subject to the following restrictions:
*
* 1. The author is not responsible for any consequences of use of
* this software, even if they arise from flaws in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission. Credits must appear in the
* documentation.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software. Credits must
* appear in the documentation.
*
* 4. This notice may not be removed or altered.
*/
#include "cawf.h"
#ifdef UNIX
#ifdef USG
#include <string.h>
#else
#include <strings.h>
#endif
#include <sys/stat.h>
#else
#include <io.h>
#include <process.h>
#include <string.h>
#include <sys\types.h>
#include <sys\stat.h>
#endif
main(argc, argv)
int argc;
char *argv[];
{
char buf[MAXLINE]; /* character buffer */
char *ep; /* environment pointer */
int f = 0; /* -f option count */
char *files[MAXFILES]; /* file names */
int i; /* temporary index */
size_t l; /* length */
char *lib = CAWFLIB; /* library path */
int libl; /* library path length */
int mac = 0; /* macro specification status */
int nf = 0; /* number of files */
char *np; /* name pointer */
int pc; /* prolog count */
struct stat sbuf; /* stat buffer */
/*
* Save program name.
*/
if ((Pname = strrchr(argv[0], '\\')) != NULL)
Pname++;
else if ((Pname = strrchr(argv[0], '/')) != NULL)
Pname++;
else
Pname = argv[0];
/*
* Set error file stream pointer.
*/
Efs = stderr;
/*
* Get library name.
*/
if ((np = getenv("CAWFLIB")) != NULL)
lib = np;
libl = strlen(lib);
/*
* Get device file name.
*/
if ((ep = getenv("TERM")) == NULL || *ep == '\0')
ep = "dumb";
l = libl + 1 + strlen(ep) + strlen(".dev") + 1;
if ((np = malloc(l)) == NULL)
Error(FATAL, NOLINE, " no string space for device file name",
NULL);
(void) sprintf(np, "%s/%s.dev", lib, ep);
files[nf++] = np;
/*
* Get common text file name.
*/
l = libl + 1 + strlen("common") + 1;
if ((np = malloc(l)) == NULL)
Error(FATAL, NOLINE, " no string space for common file name",
NULL);
(void) sprintf(np, "%s/common", lib);
files[nf++] = np;
/*
* Process options.
*/
while ((i = getopt(argc, argv, "f:m:")) != EOF) {
switch (i) {
/*
* -f<font_switch>: n = none
* b = use backspaces and underlines (default)
* e = use ESC<font_code>
*/
case 'f':
switch(*optarg) {
case 'n':
case 'b':
case 'e':
Fontout = *optarg;
f++;
break;
default:
Error(WARN, NOLINE, "illegal -f option: ",
optarg);
}
break;
/*
* -m<macro_file_name>
*/
case 'm':
if (mac) {
Error(WARN, NOLINE,
"multiple macro file declaration",
NULL);
break;
}
l = libl + 2 + strlen(optarg) + strlen(".mac") + 1;
if ((np = malloc(l)) == NULL)
Error(FATAL, NOLINE, " no string space for ",
argv[1]);
(void) sprintf(np, "%s/m%s.mac", lib, optarg);
files[nf++] = np;
if (strcmp(optarg, "an") == 0)
Marg = MANMACROS;
else if (strcmp(optarg, "s") == 0)
Marg = MSMACROS;
mac++;
break;
case '?':
Err = 1;
}
}
if (f > 1)
Error(WARN, NOLINE, " conflicting font (-f) specifications");
if (Err) {
(void) fprintf(stderr,
"%s usage: -f[n|b|e] -m[macro_file] file . . .\n",
Pname);
exit(1);
}
if (mac == 0) {
/*
* No macroes - enable Bold, Italic and Roman fonts.
*/
for (i = 0; Fcode[i].nm; i++) {
switch (Fcode[i].nm) {
case 'B':
case 'I':
case 'R':
Fcode[i].status = '1';
}
}
}
/*
* Add user-supplied file names.
*/
pc = nf;
if (optind >= argc) {
files[nf++] = NULL; /* STDIN */
} else {
while (optind < argc) {
if (nf >= MAXFILES)
Error(WARN, NOLINE, " too many files at ",
argv[optind]);
files[nf++] = argv[optind++];
}
}
/*
* Make sure all input files are accessible.
*/
for (i = 0; i < nf; i++) {
if (files[i] != NULL) {
if (stat(files[i], &sbuf) != 0)
Error(WARN, NOLINE, " can't find ", files[i]);
}
}
if (Err)
exit(1);
/*
* Miscellaneous initialization.
*/
for (i = 0; ; i++) {
if (Pat[i].re == NULL)
break;
if ((Pat[i].pat = regcomp(Pat[i].re, 0)) == NULL)
Error(WARN, NOLINE, Pat[i].re, " regcomp failure");
}
if ((i = Findscale('n', 0.0, 0)) < 0)
Error(WARN, NOLINE, " can't find Scale['n']", NULL);
Scalen = Scale[i].val;
if ((i = Findscale('u', 0.0, 0)) < 0)
Error(WARN, NOLINE, " can't find Scale['u']", NULL);
Scaleu = Scale[i].val;
if ((i = Findscale('v', 0.0, 0)) < 0)
Error(WARN, NOLINE, " can't find Scale['v']", NULL);
Scalev = Scale[i].val;
(void) Findstr("CH", "= % -", 1);
Cont = Newstr(" ");
Contlen = 1;
if ((Trtbl = (char *)malloc(256)) == NULL)
Error(WARN, NOLINE, " can't allocate translate table space",
NULL);
else {
*Trtbl = ' ';
for (i = 1; i < 256; i++)
Trtbl[i] = (char) i;
}
if (Err)
exit(1);
/*
* Here begins pass1 of awf - reading input lines and expandi